package com.gdk.ksoft.ui.reporters;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gdk.ksoft.R;
import com.gdk.ksoft.assembly.dbhelper.FreporterDbHelper;
import com.gdk.ksoft.ui.reporters.adapters.DebtStatementAdapter;
import com.gdk.ksoft.assembly.utils.extensions.FormatUtils;
import com.gdk.ksoft.assembly.utils.system.SQLiteUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.HashMap;
import java.util.List;

/**
 * 客户历史欠款查询页面
 */
public class DebtStatementActivity extends Activity {

    @ViewInject(R.id.pdebtStatement_etSearch)private EditText etSearch;//搜索框
    @ViewInject(R.id.pdebtStatement_txtDebtCount)private TextView txtDebtCount;//欠款张数
    @ViewInject(R.id.pdebtStatement_txtDebtMoney)private TextView txtDebtMoney;//欠款金额
    @ViewInject(R.id.pdebtStatement_lstDebt)private ListView lstDebt;//客户欠款数据
    private DbUtils dbUtils;//数据库操作对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_debt_statement);
        ViewUtils.inject(this);
        dbUtils = SQLiteUtils.getInstance(this).getDbUtils();
    }

    @OnClick(value={R.id.pdebtStatement_txtSearch})
    public void onViewClick(View v){
        switch(v.getId()){
            case R.id.pdebtStatement_txtSearch:
                String keyword = etSearch.getText().toString();
                if(TextUtils.isEmpty(keyword)){
                    Toast.makeText(this,"请输入查询的关键字!",Toast.LENGTH_SHORT).show();
                    return;
                }
                findAllHistoryDebt(this,keyword);//查询历史欠款信息
                break;
            default:
                break;
        }
    }

    /**
     * 查询历史欠款信息
     * @param context
     * @param keyword
     */
    public void findAllHistoryDebt(final Context context,final String keyword){
        new AsyncTask<Void,Void,Void>(){

            private List<HashMap<String,Object>> list = null;
            private String errMessage = null;
            private ProgressDialog dialog = null;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                dialog = ProgressDialog.show(context,"","正在查询...");
            }

            @Override
            protected Void doInBackground(Void... params) {
                try{
                    list = FreporterDbHelper.findAllHistoryDebt(dbUtils,keyword);
                }catch (Exception e){
                    errMessage = e.getMessage();
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                if(dialog!=null && dialog.isShowing()) dialog.dismiss();
                if(errMessage!=null){
                    Toast.makeText(context,"查询失败!原因："+errMessage,Toast.LENGTH_SHORT).show();
                    return;
                }
                if(list == null||list.size()<=0){
                    Toast.makeText(context,"当前没有欠款！",Toast.LENGTH_SHORT).show();
                    return;
                }
                makeAccountDebtInfo(list);//统计欠款信息汇总
                lstDebt.setAdapter(new DebtStatementAdapter(context,list));
            }

        }.execute();
    }

    /**
     * 统计欠款信息汇总
     */
    public void makeAccountDebtInfo(List<HashMap<String,Object>> list){
        if(list!=null && list.size()>0){
            int count = 0;//统计张数
            float wsje = 0f;//未收金额总数
            for(HashMap<String,Object> item : list){
                count++;//张数增加
                float tmpWsje = 0f;//未收金额
                try {tmpWsje = Float.parseFloat(item.get("kwsje").toString());}
                catch (Exception e){e.printStackTrace();}
                wsje+=tmpWsje;//统计未收金额
            }
            txtDebtCount.setText(count+"张");
            txtDebtMoney.setText(FormatUtils.exactNumber(wsje,"0.00")+"元");
        }
    }

    @OnClick(value={R.id.pdebtStatement_imgBack})
    public void onBackClick(View view){
        this.finish();
        overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
            this.finish();
            overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
